package org.reyton.action;

import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.ServletActionContext;
import org.reyton.entity.*;
import org.reyton.service.BizClaimVoucherDetailService;
import org.reyton.service.BizClaimVoucherService;
import org.reyton.service.SysDictionaryService;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.util.List;

/**
 * 报销单表控制器
 * Created by reyton on 16-12-1.
 */
@Controller("bizClaimVoucherAction")
@Scope("prototype")
public class BizClaimVoucherAction extends ActionSupport {
    @Resource(name = "bizClaimVoucherService")
    private BizClaimVoucherService bizClaimVoucherService;
    @Resource(name = "sysDictionaryService")
    private SysDictionaryService sysDictionaryService;
    @Resource(name = "bizClaimVoucherDetailService")
    private BizClaimVoucherDetailService bizClaimVoucherDetailService;
    private BizClaimVoucher bizClaimVoucher;
    private BizClaimVoucherDetail bizClaimVoucherDetail;
    private BizClaimVoucherDetail bizClaimVoucherDetail1;
    private List<BizClaimVoucherDetail> bizClaimVoucherDetailList;

    private PageBean<BizClaimVoucher> pageBean;
    private int pageSize = 5;
    private int pageIndex = 1;



    /***
     * 初始化页面
     * @return Action结果
     * @throws Exception
     */
    public String initPage() throws Exception{
        list();
        return "initPage_success";
    }

    /***
     * 查看报销单
     * @return Action结果
     * @throws Exception
     */
    public String checkView()throws Exception{
        bizClaimVoucher = bizClaimVoucherService.findById(bizClaimVoucher);
        bizClaimVoucherDetailList = bizClaimVoucherDetailService.findDetails(bizClaimVoucher);
        ServletActionContext.getRequest().getSession().setAttribute("bizClaimVoucher",bizClaimVoucher);
        ServletActionContext.getRequest().getSession().setAttribute("bizClaimVoucherDetailList",bizClaimVoucherDetailList);
        return "checkView_success";
    }

    /***
     * 分页查询
     * @return Action结果
     * @throws Exception
     */
    public void list(){
        SysEmployee sysEmployee = (SysEmployee) ServletActionContext.getRequest().getSession().getAttribute("EMP");
        pageBean = bizClaimVoucherService.findPageById(sysEmployee,pageSize,pageIndex);
        ServletActionContext.getRequest().getSession().setAttribute("pageBean",pageBean);
    }

    /***
     * 提交审核
     * @return Action结果
     * @throws Exception
     */
    public String submit() throws Exception{
        if(bizClaimVoucher.getCreateTime() == null){
            bizClaimVoucher = bizClaimVoucherService.findById(bizClaimVoucher);
        }
        SysDictionary sysDictionary = new SysDictionary();
        sysDictionary.setId(2);
        sysDictionary.setValue("已提交");
        bizClaimVoucher.setSysDictionary(sysDictionary);
        bizClaimVoucherService.updateStatus(bizClaimVoucher);
        ServletActionContext.getRequest().getSession().setAttribute("bizClaimVoucher",bizClaimVoucher);
        return "submit_success";
    }

    /***
     * 新增报销单明细
     * @return Action结果
     * @throws Exception
     */
    public String saveDetail() throws Exception {
        if (bizClaimVoucher.getId() == null) {
            createClaimVoucher();
        }
        //bizClaimVoucherService.saveClaimVoucherAndDetail(isFirstCreate, bizClaimVoucher, bizClaimVoucherDetail);
        bizClaimVoucherDetail.setBizClaimVoucher(bizClaimVoucher);
        bizClaimVoucherDetailService.saveDetail(bizClaimVoucherDetail);
        bizClaimVoucherDetailList = bizClaimVoucherDetailService.findDetails(bizClaimVoucher);
        count();
        ServletActionContext.getRequest().getSession().setAttribute("bizClaimVoucher",bizClaimVoucher);
        ServletActionContext.getRequest().getSession().setAttribute("bizClaimVoucherDetailList",bizClaimVoucherDetailList);
        return "saveDetail_success";
    }

    /***
     * 创建报销单
     */
    public void createClaimVoucher() {
        SysEmployee emp = (SysEmployee) ServletActionContext.getRequest().getSession().getAttribute("EMP");
        SysDictionary sysDictionary = new SysDictionary();
        sysDictionary.setId(1);
        //下列属性已从页面获取
        //bizClaimVoucher.setId();
        //bizClaimVoucher.setCreateTime();
        //bizClaimVoucher.setEvent();
        bizClaimVoucher.setSysEmployeeByCreateSn(emp);
        bizClaimVoucher.setSysDictionary(sysDictionary);
        if(emp.getSysPosition().getId() == 3){
            SysEmployee sysEmployee = new SysEmployee();
            sysEmployee.setSn("1000");
            bizClaimVoucher.setSysEmployeeByNextDealSn(sysEmployee);
        }else {
            bizClaimVoucher.setSysEmployeeByNextDealSn(emp.getSysDepartment().getSysEmployee());
        }
        bizClaimVoucher.setTotalAccount(0);
        bizClaimVoucherService.saveClaimVoucher(bizClaimVoucher);
    }

    /***
     * 删除报销单（级联删除明细）
     * @throws Exception
     */
    public String deleteClaimVoucher() throws Exception{
        bizClaimVoucher.getId();
        bizClaimVoucherService.deleteClaimVoucher(bizClaimVoucher);
        ServletActionContext.getRequest().getSession().setAttribute("Message","删除成功！");
        return "deleteClaimVoucher_success";
    }

    /**
     * 删除报销单明细
     * @return Action结果
     * @throws Exception
     */
    public String deleteDetail() throws Exception {
        bizClaimVoucherDetail.setBizClaimVoucher(bizClaimVoucher);
        bizClaimVoucherDetailService.deleteDetails(bizClaimVoucherDetail);
        bizClaimVoucherDetailList = bizClaimVoucherDetailService.findDetails(bizClaimVoucher);
        count();
        ServletActionContext.getRequest().getSession().setAttribute("bizClaimVoucher",bizClaimVoucher);
        ServletActionContext.getRequest().getSession().setAttribute("bizClaimVoucherDetailList",bizClaimVoucherDetailList);
        return "deleteDetail_success";
    }

    /***
     * 修改报销单明细
     * @return
     * @throws Exception
     */
    public String updateDetail() throws Exception{
        bizClaimVoucherDetail1.setBizClaimVoucher(bizClaimVoucher);
        bizClaimVoucherDetailService.editDetails(bizClaimVoucherDetail1);
        bizClaimVoucherDetailList = bizClaimVoucherDetailService.findDetails(bizClaimVoucher);
        count();ServletActionContext.getRequest().getSession().setAttribute("bizClaimVoucher",bizClaimVoucher);
        ServletActionContext.getRequest().getSession().setAttribute("bizClaimVoucherDetailList",bizClaimVoucherDetailList);
        return "updateDetail_success";
    }

    /***
     * 计算报销单总价
     */
    public void count() {
        int account = 0;
        for (BizClaimVoucherDetail b : bizClaimVoucherDetailList) {
            account += b.getAccount();
        }
        this.bizClaimVoucher.setTotalAccount(account);
        bizClaimVoucherService.updateAccount(this.bizClaimVoucher);
        bizClaimVoucherService.updateDes(bizClaimVoucher);
    }

    public PageBean<BizClaimVoucher> getPageBean() {
        return pageBean;
    }

    public void setPageBean(PageBean<BizClaimVoucher> pageBean) {
        this.pageBean = pageBean;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public int getPageIndex() {
        return pageIndex;
    }

    public void setPageIndex(int pageIndex) {
        this.pageIndex = pageIndex;
    }

    public BizClaimVoucherDetail getBizClaimVoucherDetail1() {
        return bizClaimVoucherDetail1;
    }

    public void setBizClaimVoucherDetail1(BizClaimVoucherDetail bizClaimVoucherDetail1) {
        this.bizClaimVoucherDetail1 = bizClaimVoucherDetail1;
    }

    public List<BizClaimVoucherDetail> getBizClaimVoucherDetailList() {
        return bizClaimVoucherDetailList;
    }

    public void setBizClaimVoucherDetailService(BizClaimVoucherDetailService bizClaimVoucherDetailService) {
        this.bizClaimVoucherDetailService = bizClaimVoucherDetailService;
    }

    public void setBizClaimVoucherDetailList(List<BizClaimVoucherDetail> bizClaimVoucherDetailList) {
        this.bizClaimVoucherDetailList = bizClaimVoucherDetailList;
    }

    public void setSysDictionaryService(SysDictionaryService sysDictionaryService) {
        this.sysDictionaryService = sysDictionaryService;
    }

    public BizClaimVoucherService getBizClaimVoucherService() {
        return bizClaimVoucherService;
    }

    public void setBizClaimVoucherService(BizClaimVoucherService bizClaimVoucherService) {
        this.bizClaimVoucherService = bizClaimVoucherService;
    }

    public BizClaimVoucher getBizClaimVoucher() {
        return bizClaimVoucher;
    }

    public void setBizClaimVoucher(BizClaimVoucher bizClaimVoucher) {
        this.bizClaimVoucher = bizClaimVoucher;
    }

    public BizClaimVoucherDetail getBizClaimVoucherDetail() {
        return bizClaimVoucherDetail;
    }

    public void setBizClaimVoucherDetail(BizClaimVoucherDetail bizClaimVoucherDetail) {
        this.bizClaimVoucherDetail = bizClaimVoucherDetail;
    }
}
